package com.gwclh.personnel.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Department;
import com.gwclh.personnel.entity.Employee;
import com.gwclh.personnel.entity.Station;
import com.gwclh.personnel.mapper.DepartmentMapper;
import com.gwclh.personnel.mapper.EmployeeMapper;
import com.gwclh.personnel.service.IDepartmentService;
import com.gwclh.personnel.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.service.IStationService;
import com.gwclh.personnel.vo.employeevo.BaseEmployeeVo;
import com.gwclh.personnel.vo.employeevo.EmployeeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    IDepartmentService departmentService;
    @Autowired
    IStationService stationService;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public Map<String, Object> pageEmployees(PageDomain pageDomain, Employee employee) {
        Map<String, Object> resultMap = new HashMap<>();
        List<EmployeeVo> resultList = new ArrayList<>();

        Page<Employee> employeePage = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        employeePage.addOrder(pageDomain.getOrderItemBy());
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("is_talents", false);
        employeeQueryWrapper.eq("status", 1);
        if (!StrUtil.isEmpty(employee.getEmpName())) {
            employeeQueryWrapper.like("emp_name", employee.getEmpName());
        }
        if (employee.getDeptId() != null) {
            employeeQueryWrapper.eq("dept_id", employee.getDeptId());
        }
        if (employee.getStaId() != null) {
            employeeQueryWrapper.eq("sta_id", employee.getStaId());
        }
        if (employee.getEmpType() != null) {
            employeeQueryWrapper.eq("emp_type", employee.getEmpType());
        }
        if (employee.getIsTalents() != null) {
            employeeQueryWrapper.eq("is_talents", employee.getIsTalents());
        }
        if (employee.getIdCard() != null) {
            employeeQueryWrapper.eq("id_card", employee.getIdCard());
        }
        this.page(employeePage, employeeQueryWrapper);

        employeePage.getRecords().forEach(e -> {
            EmployeeVo employeeVo = new EmployeeVo();
            BeanUtils.copyProperties(e, employeeVo);
            employeeVo.setDepartment(departmentService.getById(e.getDeptId()));
            employeeVo.setStation(stationService.getById(e.getStaId()));
            resultList.add(employeeVo);
        });
        resultMap.put("total", employeePage.getTotal());
        resultMap.put("employees", resultList);
        return resultMap;
    }

//    public ResultVo statisticsEntryEmpByCompany(Integer pId) {
//        List<Map<String, Object>> resultList = employeeMapper.statisticsEntryEmpByCompany(pId);
//        // resultList.stream().
//        return ResultVo.success(resultList);
//    }

    @Override
    public Map<String, Object> pageTalents(PageDomain pageDomain, Employee employee) {
        Map<String, Object> resultMap = new HashMap<>();
        List<EmployeeVo> resultList = new ArrayList<>();

        Page<Employee> employeePage = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        employeePage.addOrder(pageDomain.getOrderItemBy());
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(employee.getEmpName())) {
            employeeQueryWrapper.like("emp_name", employee.getEmpName());
        }
        employeeQueryWrapper.eq("is_talents", true);

        this.page(employeePage, employeeQueryWrapper);

        employeePage.getRecords().forEach(e -> {
            EmployeeVo employeeVo = new EmployeeVo();
            BeanUtils.copyProperties(e, employeeVo);
            employeeVo.setDepartment(departmentService.getById(e.getDeptId()));
            employeeVo.setStation(stationService.getById(e.getStaId()));
            resultList.add(employeeVo);
        });
        resultMap.put("total", employeePage.getTotal());
        resultMap.put("employees", resultList);
        return resultMap;
    }

    @Override
    public ResultVo listEmployeesToDept(PageDomain pageDomain, Employee employee) {
        Page<Employee> employeePage = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        employeePage.addOrder(pageDomain.getOrderItemBy());
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        if (!StrUtil.isEmpty(employee.getEmpName())) {
            queryWrapper.like("emp_name", employee.getEmpName());
        }
        if (employee.getDeptId() != null) {
            queryWrapper.eq("dept_id", employee.getDeptId());
        }
        if (employee.getId() != null) {
            queryWrapper.eq("id", employee.getId());
        }
        this.page(employeePage, queryWrapper);
        List<BaseEmployeeVo> employeeVos = new ArrayList<>();
        Map<String, Object> result = new HashMap<>();
        employeePage.getRecords().forEach(e -> {
            BaseEmployeeVo employeeVo = new BaseEmployeeVo();
            BeanUtils.copyProperties(e, employeeVo);
            Department department = departmentService.getOne(new QueryWrapper<Department>().eq("id", employeeVo.getDeptId()));
            if (department != null) {
                employeeVo.setDeptName(department.getDeptName());
            }
            Station station = stationService.getOne(new QueryWrapper<Station>().eq("id", employeeVo.getStaId()));
            if (station != null) {
                employeeVo.setStaName(station.getStaName());
            }
            employeeVos.add(employeeVo);
        });
        result.put("employeeVos", employeeVos);
        result.put("total", employeePage.getTotal());
        return ResultVo.success(result);

    }

    public ResultVo statisticsEntryEmpByCompany(Integer pId) {
        List<Map<String, Object>> resultList = employeeMapper.statisticsEntryEmpByCompany(pId);
        //        resultList.stream().
        return ResultVo.success(resultList);
    }

    @Override
    public List<Map<String, Object>> getInternshipMonthDate() {
        return employeeMapper.selectInternshipMonthData();
    }

    @Override
    public List<Map<String, Object>> getEntryDataByPid(Integer pId) {
        List<Map<String, Object>> result = new ArrayList<>();
        List<Department> departments = departmentMapper.selectList(new QueryWrapper<Department>().eq("p_id", pId));
        departments.forEach(department -> {
            Map<String, Object> map = employeeMapper.selectEntryDataByDeptId(department.getId());
            map.put("name", department.getDeptName());
            result.add(map);
        });
        return result;
    }

    // /**
    // * 查询整个公司的入职人数
    // *
    // * @return
    // */
    // @Override
    // public Map<String, Object> statisticsEntryEmpMonth() {
    // return employeeMapper.statisticsEntryEmpMonth();
    // }

    // /**
    // * 查询某公司下各部门有多少员工
    // *
    // * @param pId
    // * @return
    // */
    // @Override
    // public ResultVo statisticsDeptEmpByCompany(Integer pId) {
    // List<Map<String, Object>> result =
    // employeeMapper.statisticsDeptEmpByCompany(pId);
    // return ResultVo.success(result);
    // }

    // public ResultVo statisticsEntryEmpByCompany(Integer pId) {
    // List<Map<String, Object>> resultList =
    // employeeMapper.statisticsEntryEmpByCompany(pId);
    // // resultList.stream().
    // return ResultVo.success(resultList);
    // }

    /**
     * 查询整个公司的入职人数
     *
     * @return
     */
    @Override
    public Map<String, Object> statisticsEntryEmpMonth() {
        return employeeMapper.statisticsEntryEmpMonth();
    }

    /**
     * 查询某公司下各部门有多少员工
     *
     * @param pId
     * @return
     */
    @Override
    public ResultVo statisticsDeptEmpByCompany(Integer pId) {
        List<Map<String, Object>> result = employeeMapper.statisticsDeptEmpByCompany(pId);
        return ResultVo.success(result);
    }

    @Override
    public ResultVo getEmpTranLog(Integer id) {
        Employee employee = this.getById(id);
        BaseEmployeeVo baseEmployeeVo = new BaseEmployeeVo();
        BeanUtils.copyProperties(employee, baseEmployeeVo);
        Department department = departmentService.getOne(new QueryWrapper<Department>().eq("id", baseEmployeeVo.getDeptId()));
        if (department != null) {
            baseEmployeeVo.setDeptName(department.getDeptName());
        }
        Station station = stationService.getOne(new QueryWrapper<Station>().eq("id", baseEmployeeVo.getStaId()));
        if (station != null) {
            baseEmployeeVo.setStaName(station.getStaName());
        }
        System.out.println("++++" + baseEmployeeVo.toString());
        return ResultVo.success(baseEmployeeVo);
    }

    /**
     * 统计岗位在岗人数
     *
     * @return
     */
    @Override
    public ResultVo statisticsStationEmp() {
        return ResultVo.success(employeeMapper.statisticsStationEmp());
    }
}
